﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SATF.BI;
using System.Data;
using System.Collections;
using ExtAspNet;

namespace SATF
{
    public partial class NewTaskPage1 : System.Web.UI.Page
    {
        private BIUser user = null;
        private static IList<BIMachine> machines = null;
        private static IList<BIMachine> machineGridSource = null;
        private static IList<BIMachine> selectedMachines = null;
        private static ExtAspNet.TreeNode lastCaseNode = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!AssertIsLogon())
            {
                return;
            }
            string userName = Session["user"].ToString();

            if (!IsPostBack)
            {
                selectedMachines = new List<BIMachine>();
                user = LogonInfosSingleton.instance[userName].BIUser;
                LoadData();
            }
        }

        private void LoadData()
        {
            machines = this.user.GetMachines();
            this.MachineGrid.DataSource = machines;
            machineGridSource = machines;
            this.MachineGrid.DataBind();
            CaseTreeUtil.CreateTree(this.CaseTree, user);
        }

        protected void MachineTree_NodeCheck(object sender, ExtAspNet.TreeCheckEventArgs e)
        {
            if (e.Checked)
            {
                CaseTree.CheckAllNodes(e.Node.Nodes);
            }
            else
            {
                CaseTree.UncheckAllNodes(e.Node.Nodes);
            }

        }

        protected void CaseTree_NodeCommand(object sender, ExtAspNet.TreeCommandEventArgs e)
        {
           /* if (lastCaseNode != null)
            {
                this.CaseTree.FindNode(lastCaseNode.NodeID).Text = lastCaseNode.Text.Replace("'highlight'", "'nonestyle'");
            }
            if (e.Node.Leaf == true)
            {
                e.Node.Text = e.Node.Text.Replace("'nonestyle'", "'highlight'");
            }
            CaseTree.BodyStyle
            lastCaseNode = e.Node;*/
        }


        protected void OkButton_Click(object sender, EventArgs e)
        {
            
        }

        protected void MachineGrid_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            machineGridSource = SortHelper.IListOrderBy(machineGridSource, e.SortField, e.SortDirection);
            this.MachineGrid.DataSource = machineGridSource;
            this.MachineGrid.DataBind();
      
        }

        protected void SelectMachineButton_Click(object sender, EventArgs e)
        {
            foreach (int index in this.MachineGrid.SelectedRowIndexArray)
            {
                BIMachine machine = machineGridSource[index];
                var result = 
                    from item in selectedMachines
                    where item.ID == machine.ID && item.ID > 0
                    select item;

                if (result.Count() == 0)
                {
                    selectedMachines.Add(machine);
                }
            }

            this.SelectedMachineGrid.DataSource = selectedMachines;
            this.SelectedMachineGrid.DataBind();
        }

        protected void RemoveMachineButton_Click(object sender, EventArgs e)
        {
            int offset = 0;
            foreach (int index in this.SelectedMachineGrid.SelectedRowIndexArray)
            {

                selectedMachines.RemoveAt(index - offset);
                offset++;
            }

            this.SelectedMachineGrid.DataSource = selectedMachines;
            this.SelectedMachineGrid.DataBind();
        }

        protected void AllMachineButton_Click(object sender, EventArgs e)
        {
            machineGridSource = machines;
            this.MachineGrid.DataSource = machineGridSource;
            this.MachineGrid.DataBind();
        }

        // 点击 SearchTriggerBox 的搜索按钮
        protected void SearchTriggerBox_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.SearchTriggerBox.Text))
            {
                // 执行搜索动作
                string keyWord = this.SearchTriggerBox.Text;
                if (keyWord != "")
                {
                    var result =
                        from item in machines
                        where item.ID.ToString().Contains(keyWord)
                            || item.IP.ToString().Contains(keyWord)
                            || item.Name.ToString().Contains(keyWord)
                            || item.UserName.ToString().Contains(keyWord)
                            || item.GroupName.ToString().Contains(keyWord)
                            || item.State_Str.ToString().Contains(keyWord)
                        select item;

                    if (result.Count() != 0)
                    {
                        machineGridSource = new List<BIMachine>();
                        foreach (var i in result)
                        {
                            machineGridSource.Add(i as BIMachine);
                        }
                        this.MachineGrid.DataSource = machineGridSource;
                        this.MachineGrid.DataBind();
                    }
                    else
                    {
                        Alert.ShowInTop("未检索到相关记录!");
                    }
                }
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }

        // 点击 SearchTriggerBox 的取消按钮
        protected void SearchTriggerBox_Trigger1Click(object sender, EventArgs e)
        {
            this.SearchTriggerBox.Text = "";
            this.SearchTriggerBox.ShowTrigger1 = false;

        }


        protected void AutoRunCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (this.AutoRunCheckBox.Checked)
            {
                this.AutoRunGroupPanel.Enabled = true;
                this.AutoRunGroupPanel.Collapsed = false;
            }
            else
            {
                this.AutoRunGroupPanel.Collapsed = true;
                this.AutoRunGroupPanel.Enabled = false;
            }

        }


        protected void CaseSplitCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (this.CaseSplitCheckBox.Checked)
            {
                this.CreateCaseCfgButton.Enabled = true;
                this.CaseSplitGrid.Hidden = false;
            }
            else
            {
                this.CreateCaseCfgButton.Enabled = false;
                this.CaseSplitGrid.Hidden = true;
                
               
            }

        }


      /*  protected void SearchMachineButton_Click(object sender, EventArgs e)
        {
            string keyWord = this.SearchMachineTextBox.Text;
            if (keyWord != "")
            {
                var result =
                    from item in machines
                    where item.ID.ToString().Contains(keyWord)
                        || item.IP.ToString().Contains(keyWord)
                        || item.Name.ToString().Contains(keyWord)
                        || item.UserName.ToString().Contains(keyWord)
                        || item.GroupName.ToString().Contains(keyWord)
                        || item.State_Str.ToString().Contains(keyWord)
                    select item;

                if (result.Count() != 0)
                {
                    machineGridSource = new List<BIMachine>();
                    foreach (var i in result)
                    {
                        machineGridSource.Add(i as BIMachine);
                    }
                    this.MachineGrid.DataSource = machineGridSource;
                    this.MachineGrid.DataBind();
                }
                else
                {
                    Alert.ShowInTop("未检索到相关记录!");
                }
            }
        }
        */
            


        private bool AssertIsLogon()
        {
            if (Session["user"] == null || Session["user"].ToString() == "")
            {
                return false;
            }
            string userName = Session["user"].ToString();

            LogonInfo info = LogonInfosSingleton.instance[userName];
            if (info == null || !info.IsLogon)
            {
                return false;
            }
            return true;
        }

    }
}